import kdform from "./components/kdForm.vue";
import kdcrud from "./components/kdcrud.vue";
import kdlayout from "./components/kdlayout.vue";
import * as api from "@/utils/api";

// 导入forms文件夹下的所有组件
const formComponents = import.meta.globEager("./components/forms/*.vue");
// 导入layout文件夹下的所有组件
const layoutComponents = import.meta.globEager("./components/layout/*.vue");
const install = (Vue) => {
  Vue.component("kdform", kdform);
  Vue.component("kdcrud", kdcrud);
  Vue.component("kdlayout", kdlayout);
  // 合并到一起，通过遍历完成全局组件的创建
  Object.assign(formComponents, layoutComponents);
  Object.keys(formComponents).forEach((item) => {
    const comp = formComponents[item].default;
    const name = item.split("/")[3].split(".")[0];
    Vue.component(name, comp);
  });

  // 全局挂载请求api
  for (let key in api) {
    if (api[key]) Vue.config.globalProperties[key] = api[key];
  }
  // 进入全屏
  Vue.config.globalProperties["fullScreen"] = () => {
    let element = document.documentElement || document.body;
    if (element.requestFullscreen) {
      element.requestFullscreen();
    } else if (element.webkitRequestFullScreen) {
      element.webkitRequestFullScreen();
    } else if (element.mozRequestFullScreen) {
      element.mozRequestFullScreen();
    } else if (element.msRequestFullscreen) {
      element.msRequestFullscreen();
    }
  };
  // 退出全屏
  Vue.config.globalProperties["exitScreen"] = () => {
    if (document.fullscreen) {
      if (document.exitFullscreen) {
        document.exitFullscreen();
      } else if (document.webkitCancelFullScreen) {
        document.webkitCancelFullScreen();
      } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
      } else if (document.msExitFullscreen) {
        document.msExitFullscreen();
      }
    }
  };
};
export default install;
